/*
 * Ellipse.java
 *
 * Created on 9 maj 2007, 19:02
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package pl.krakow.ae.knp.wikiboard.gui.drawables;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import pl.krakow.ae.knp.wikiboard.gui.board.Board;
import pl.krakow.ae.knp.wikiboard.utils.GraphicalUtils;

/**
 *
 *
 */

public class Ellipse extends Figure
{
    private Point2D startPoint;
    private Ellipse2D ellipse;
    private Color outlineColor, interiorColor;
    
    /** Creates a new instance of Ellipse */
    public Ellipse(Point2D startingPoint, Color color)
    {
        super();
        ellipse = new Ellipse2D.Double(startingPoint.getX(), startingPoint.getY(), 0, 0);
        startPoint = startingPoint;
        //setting transparency
        interiorColor = GraphicalUtils.createTransparentColor(Board.getBgColor());
        outlineColor = color;
    }
    
    protected Ellipse(Ellipse e)
    {
        super();
        this.startPoint = (Point2D)e.startPoint.clone();
        this.ellipse = (Ellipse2D)e.ellipse.clone();
        this.outlineColor = GraphicalUtils.cloneColor(e.outlineColor);
        this.interiorColor = GraphicalUtils.cloneColor(e.interiorColor);
    }
    
    public void rotate()
    {
    }
    
    public boolean contains(Point2D p)
    { return ellipse.intersects(p.getX() - 1, p.getY() - 1, 3, 3); }
    
    public void draw(Graphics2D g, boolean dragged)
    {
        g.setXORMode(Board.getBgColor());
        if(!dragged)
            g.setPaintMode();
        g.setColor(interiorColor);
        g.fill(ellipse);
        g.setColor(outlineColor);
        g.draw(ellipse);
        
        if (isSelected)
        {
            Graphics2D g2 = (Graphics2D)g.create();
            g2.setColor(Board.getSelectColor());
            g2.setStroke(new BasicStroke(STROKE_WIDTH));
            Rectangle2D outline = ellipse.getBounds2D();
            g2.drawRect( (int)outline.getX() - SELECT_PADDING,
                    (int)outline.getY() - SELECT_PADDING,
                    (int)outline.getWidth() + 2*SELECT_PADDING,
                    (int)outline.getHeight() + 2*SELECT_PADDING);
            g2.dispose();
        }
    }
    
    public void move(double x, double y)
    {
        ellipse.setFrame(ellipse.getX()+x, ellipse.getY()+y, ellipse.getWidth(),
                ellipse.getHeight());
    }
    
    public void drag(MouseEvent event)
    {
        Point2D p = event.getPoint();
        ellipse = newEllipse2D(p.getX(), p.getY(), event.isShiftDown());
    }
    
    private Ellipse2D.Double newEllipse2D(double endX, double endY,
            boolean isShiftDown)
    {
        double startX = startPoint.getX();
        double startY = startPoint.getY();
        double width = Math.abs(startX - endX);
        double height = Math.abs(startY - endY);
        
        if(isShiftDown)
        {
            double side=Math.max(width, height);
            double x = startX <= endX ? startX : startX-side;
            double y = startY <= endY ? startY : startY-side;
            
            return new Ellipse2D.Double(x, y, side, side);
        }
        else
        {
            double x = startX < endX ? startX : endX;
            double y = startY < endY ? startY : endY;
            
            return new Ellipse2D.Double(x, y, width, height);
        }
    }
    
    public void fill(Color c)
    { interiorColor = c; }
    
    public Figure clone()
    { return new Ellipse(this); }
    
    
}